package com.cloudexpress.action;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.cloudexpress.action.bo.UserBo;
import com.cloudexpress.common.Constants;
import com.cloudexpress.common.Utils;
import com.cloudexpress.persistence.model.User;
import com.cloudexpress.service.UserService;


public class UserAction extends BaseActionSupport {
	
	@Autowired
	private UserService userService;
	
	private Logger logger = Logger.getLogger(this.getClass());

	/**
	 * 注册页面
	 * @return 
	 * @throws Exception
	 */
	public String logon() throws Exception {
		setTaskType(Constants.TASK_CREATE);
		return INPUT;
	}
	
	/**
	 * 更新页面
	 * @return
	 * @throws Exception
	 */
	public String updateInfomation() throws Exception {
		try {
			User user = userService.getUserById(getUserId());
			UserBo userBo = convertUserVoToBo(user);
			setUserBo(userBo);
			setTaskType(Constants.TASK_UPDATE);
			
			logger.error("xxxx");
			logger.info("iiiixxxx");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return INPUT;
	}
	
	/**
	 * 注册或更新提交
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		try {
			boolean isCreating = Constants.TASK_CREATE.equals(getTaskType());
			if(isCreating) {
				logger.debug("currentUser: " + getUserBo());
				User currentUser = converUserBoToVo(getUserBo());
				List<User> thisEmailUserList = userService.getUserByEmail(currentUser.getEmail());
				if(!thisEmailUserList.isEmpty()) {
					addActionError(getText("error.email.exists"));
					return INPUT;
				}
				
				currentUser.setPassword(Utils.encryptPassword(currentUser.getPassword()));
				currentUser.setDateJoin(new Date());
				userService.insertUser(currentUser);
			} else {
				User user = userService.getUserById(getUserId());
				
				//TODO need to check password
				User userUpdate = converUserBoToVo(getUserBo());
				user.setAddress(userUpdate.getAddress());
				user.setEmail(userUpdate.getEmail());
				user.setName(userUpdate.getName());
				user.setPassword(Utils.encryptPassword(userUpdate.getPassword()));
				user.setPhone(userUpdate.getPassword());
				user.setQq(userUpdate.getQq());
				userService.udpateUser(user);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		
		return SUCCESS;
	}
	
	
	private UserBo convertUserVoToBo(User user) throws IllegalAccessException, InvocationTargetException {
		UserBo userBo = new UserBo();
		copyProperties(userBo, userBo);
		return userBo;
	}
	
	private User converUserBoToVo(UserBo userBo) throws IllegalAccessException, InvocationTargetException{
		User user = new User();
		copyProperties(user, userBo);
		return user;
	}
		
	private String taskType;
	
	private int userId;
	
	private UserBo userBo;

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}
	
	public UserBo getUserBo() {
		return userBo;
	}

	public void setUserBo(UserBo userBo) {
		this.userBo = userBo;
	}

	public String getTaskType() {
		return taskType;
	}

	public void setTaskType(String taskType) {
		this.taskType = taskType;
	}
	
	
	
}
